package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.dao.MemberDao;
import com.atguigu.pojo.Member;
import com.atguigu.service.MemberService;
import com.atguigu.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    MemberDao memberDao;

    @Override
    public Member findMemberByPhoneNumber(String telephone) {
        Member member = memberDao.findMemberByTelephone(telephone);
        return member;
    }

    @Override
    public void add(Member member) {
        memberDao.add(member);
    }

    @Override
    public List<Integer> findMemberCountByMonth(List<String> months) {
        if (months != null && months.size() > 0) {
            List<Integer> memberCountList = new ArrayList<>();
            for (String month : months) {
                // 获取当前月的最后一天
                String lastDayOfMonth = DateUtils.getLastDayOfMonth(month);
                Integer memberCount = memberDao.findMemberCountBeforeDate(lastDayOfMonth);
                memberCountList.add(memberCount);
            }
            return memberCountList;
        }
        return null;
    }
}
